/*
 * @Author: Lee 497232807@qq.com
 * @Date: 2023-07-18 17:15:12
 * @LastEditors: Lee 497232807@qq.com
 * @LastEditTime: 2023-07-26 15:48:42
 * @FilePath: \ccc-hot-update\assets\script\view\mainScene.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Button, Sprite, native } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { Dirction, Utils } from '../common/Utils';
import { Prefab } from 'cc';
import { instantiate } from 'cc';
import { tween } from 'cc';
import { v3 } from 'cc';
import { view } from 'cc';
import { Label } from 'cc';
import { SubGameState, VersionManager } from '../common/VersionManager';
import { NATIVE } from 'cc/env';
import { AppInfo } from '../common/AppInfo';

const { ccclass, property } = _decorator;

@ccclass('mainScene')
export class mainScene extends Component {

    @property(Button)
    btn_game: Button = null;

    /**
     * 屏幕旋转测试
     */
    @property(Button)
    btn_ratate: Button = null;
    /**
     * 版本号
     */
    @property(Label)
    lb_version: Label = null;

    /**
     * 游戏状态
     */
    @property(Label)
    lb_state: Label = null;

     /**
     * 游戏状态
     */
     @property(Label)
     lb_appinfo: Label = null;

    /**
     * 进度条
     */
    @property(Sprite)
    sp_progress: Sprite = null;

    protected gameID = 10000;
    start() {
        this.btn_game.node.on('click', this.onBtnClick, this);
        this.btn_ratate.node.on('click', this.onRotate, this);
        if(NATIVE)
        {
            this.lb_version.string = 'version: ' + VersionManager.Instance.getLocalVersion();
            this.updateGameStatus();

            this.lb_appinfo.string = 'app版本: ' + AppInfo.getAppVersion() + ' \n 设备id:' + AppInfo.getDevicesID();
        }

    }

    protected getGameStatus(gameID:number):SubGameState {
        var status = SubGameState.UN_KNOW
        if(NATIVE)
        {
             status = VersionManager.Instance.getSubGameState(gameID);
        }
        else
        {
             status = SubGameState.NO_NEED_UPDATE
        }
        
        return status;
    }
    protected updateGameStatus() {
        var desc = ""
        if(NATIVE)
        {
            var status = this.getGameStatus(this.gameID);
            console.log('updateGameStatus', status);

            if (status == SubGameState.NO_NEED_UPDATE) {
                desc = "无需更新"
            }
            else if (status == SubGameState.NEED_UPDATE) {
                desc = "需要更新"
                this.sp_progress.fillRange = 1
                this.sp_progress.node.active = true
            }
            else if (status == SubGameState.NEED_DOWNLOAD) {
                desc = "需要下载"
                this.sp_progress.fillRange = 1
                this.sp_progress.node.active = true
            }
        }
        else
        {
            desc = "web平台不需要更新"
        }
        
        this.lb_state.string = desc
    }

    protected onRotate() {
        var dir = Utils.getOrientation();
        dir = dir == Dirction.PORTRAIT ? Dirction.LANDSCAPE : Dirction.PORTRAIT;
        Utils.changeScreenOrientation(dir);
    }
    protected onBtnClick() {
        

        var status = this.getGameStatus(this.gameID);
        if(status == SubGameState.NO_NEED_UPDATE) //不需要下载 直接进入游戏
        {
            Utils.loadBundle(this.gameID.toString(), (err, bundle) => {
                if(err)
                {
                    console.log('load bundle err', err);
                    return 
                }
                bundle.load('game',Prefab, (err, prefab) => {
                    if(err)
                    {
                        console.log('load prefab err', err);
                        return 
                    }
                    let node = instantiate(prefab);
                    node.parent = this.node;
                
                })
            });
        }
        else{//需要更新或者下载
            console.log('需要更新或者下载');
            this.btn_game.interactable = false;
            VersionManager.Instance.downLoadSubGame(this.gameID,(code:SubGameState,...args)=>{
                    if(code == SubGameState.UPDATE_SUCCESS)
                    {
                        console.log('更新成功');
                        this.updateGameStatus()
                    }
                    this.btn_game.interactable = true
                    this.sp_progress.node.active = false

            },(downloadedSize:number,totalSize:number)=>{
                var progress = downloadedSize/totalSize;
                console.log('下载进度',progress);
                this.sp_progress.fillRange = 1-progress
            })
        }
        
    }
}


